package logic

import (
	"context"
	"edu/models/mysql"
	"fmt"

	"edu/edurpc/edurpc"
	"edu/edurpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelShowLogic {
	return &NovelShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelShowLogic) NovelShow(in *edurpc.NovelShowRequest) (*edurpc.NovelShowResponse, error) {
	var novel mysql.Novel
	mysql.DB.Where("user_id = ?", in.UserId).Where("id = ?", in.NovelId).First(&novel)
	if novel.ID == 0 {
		return nil, fmt.Errorf("作品不存在")
	}
	return &edurpc.NovelShowResponse{
		Title:    novel.Title,
		Category: novel.Category,
		Info:     novel.Info,
		Refer:    novel.Refer,
		Image:    novel.Image,
	}, nil
}
